<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Goods as Goods;
use Illuminate\Support\Str;
use App\ShipmentItem as ShipmentItem;
use App\Http\Requests\GoodsCreateRequest;
use Pingpong\Admin\Uploader\ImageUploader;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class GoodsController extends Controller
{
     protected $uploader;
     public function __construct(ImageUploader $uploader)
    {
         $this->uploader = $uploader;
     }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $goods=Goods::AllOrSearch(Input::get('q'));
        return response()->view("admin.goods.index",  compact('goods'));
    }
    
    public function ajaxgoodslist(){
        $goods=Goods::Search(Input::get('q'));
        foreach($goods as $k=>$g){
            $goods[$k]["image"]=  thumber(32,$g->image);
        }
        return response()->json($goods);
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return response()->view('admin.goods.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(GoodsCreateRequest $request)
    {
        $data = $request->all();

        unset($data['image']);

        if (\Input::hasFile('image')) {
            // upload image
            $this->uploader->upload('image')->save('goods');

            $data['image'] = $this->uploader->getFilename();
        }
        $data['name'] = Str::title($data['name']);
        Goods::create($data);

        return redirect()->route('admin.goods.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        
            try{
                $goods=Goods::findOrFail($id);
                return response()->view('admin.goods.edit', compact('goods'));
            }catch (ModelNotFoundException $e) {
                return redirect()->route('admin.nofound');
            }
        
    }
    public function ajaxgetgood(){
        $id=intval(Input::get('id'));
        if($id>0){
            try{
                $goods=Goods::findOrFail($id);
                $goods->image=  thumber(60,$goods->image);
                return response()->json(array('goods'=>$goods,'status'=>1));
            }catch (ModelNotFoundException $e) {
                return response()->json(array('status'=>-1));
            }
        }
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(GoodsCreateRequest $request, $id)
    {
        try{
         $goods=Goods::findOrFail($id);
         $data = $request->all();
         unset($data['image']);
         if (\Input::hasFile('image')) {
                $this->uploader->upload('image')->save('goods');
                $data['image'] = $this->uploader->getFilename();
         }
            $data['name'] = Str::title($data['name']);
            $goods->update($data);
         return redirect()->route('admin.goods.index');
        }  catch (ModelNotFoundException $e){
            return redirect()->route('nofound');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try{
         if(ShipmentItem::where("goods_id",$id)->count()>0){
             return redirect()->route('admin.goods.index')->withFlashMessage('该产品有关联的出货单,删除失败');
         }
         $goods=Goods::findOrFail($id);
         $goods->delete();
         return redirect()->route('admin.goods.index');
        }  catch (ModelNotFoundException $e){
            return redirect()->route('nofound');
        }
    }
}
